
import os
import subprocess

class Connection(object):
    def __init__(self):
        pass
    
    def open(self, filename, mode="r"):
        ''' Must return a file object that can be manipulated like a real file object.
        '''
        return open(filename, mode)
        
    def getcwd(self):
        return os.getcwd()
    
    def cd(self, path):
        os.chdir(path)
    
    def chroot(self, root_path):
        os.chroot(root)
        
    def execute(self, exe):
        ''' Executes command on the host.
        '''
        p = subprocess.Popen(exe, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=os.getcwd())
        p.communicate()
        return p.returncode
    
    def execute_readlines(self, exe):
        p = subprocess.Popen(exe,
            bufsize=1, # line buffered (default)
            stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell, cwd=os.getcwd())
        
        while True:
            p.poll()
            line = p.stdout.readline()
            eline = p.stderr.readline()
            if line:
                yield False, line
            if eline:
                yield True, eline
            if (line == "" and eline == "" and p.returncode != None):
                break

    def put(self, src, dst):
        raise NotImplementedError
    
    def get(self, src, dst):
        raise NotImplementedError
    
    def put_fileobj(self, filelike, dst, filesize):
        raise NotImplementedError
    
    def exists(self, path):
        return os.path.exists(path)
    
        
